package cz.hanis.android.geography;

import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.XmlResourceParser;

public class ScoreHandler {

	private static final String SCORE_TAG = "score";
	private static final String SCORES_TAG = "scores";
	private static final String SCORE_ATTR = "score";
	private static final String USERNAME_ATTR = "username";

	// private SortedMap<Integer, String> scoreTable;
	private Context context;

	public ScoreHandler(Context context) {
		this.context = context;
	}

	private SortedMap<Integer, String> parseScore() {
		SortedMap<Integer, String> scoreTable = new TreeMap<Integer, String>();
		XmlResourceParser parser = context.getResources().getXml(
				R.xml.score_table);
		int eventType = -1;
		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {
				if (parser.getName().equals(SCORE_TAG)) {
					int score = parser
							.getAttributeIntValue(null, SCORE_ATTR, 0);
					String username = parser.getAttributeValue(null,
							USERNAME_ATTR);
					scoreTable.put(score, username);
				}
			}
			try {
				eventType = parser.next();
			} catch (XmlPullParserException e) {
				System.out.println("er1");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("er2");
				e.printStackTrace();
			}
		}
		return scoreTable;
	}

	public void addScore(int score, String name) {
		XmlResourceParser parser = context.getResources().getXml(
				R.xml.score_table);
		int eventType = -1;
		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {
				if (parser.getName().equals(SCORES_TAG)) {
					
				}
			}
			try {
				eventType = parser.next();
			} catch (XmlPullParserException e) {
				System.out.println("er1");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("er2");
				e.printStackTrace();
			}
		}
	}

	public SortedMap<Integer, String> getScoreTable() {
		return parseScore();
	}

}
